package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.ReservationService;
import com.zzyl.nursing.vo.ReservationResidueNumVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements ReservationService {
    @Autowired
    private ReservationMapper reservationMapper;
    @Override
    public Integer getCount() {
        long time = System.currentTimeMillis();
        //获取当前时间的LocalDataTime对象
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        //计算今天的开始时间
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        //计算今天的结束时间
        LocalDateTime endTime = startTime.plusDays(1);
        //查询status = 2,并且更新时间在今天的家属
        // sql: select * from reservation where status = 2 and update_by = #{userId} and update_time between #{start} and #{end};
        LambdaQueryWrapper<Reservation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Reservation::getStatus,2)
                .eq(Reservation::getUpdateBy, UserThreadLocal.getUserId())
                .between(Reservation::getUpdateTime,startTime,endTime);
        Integer count = (int)count(lqw);
        return count;
    }

    @Override
    public List<ReservationResidueNumVo> getSurplusNum() {
        long time = System.currentTimeMillis();
        //获取当前时间的LocalDataTime对象
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        //计算今天的开始时间
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        //计算今天的结束时间
        LocalDateTime endTime = startTime.plusDays(1);
        //每个时间段可以预约6次,超过6次该时间段不允许预约
        List<ReservationResidueNumVo> surplusNum = reservationMapper.getSurplusNum(startTime, endTime);
        return surplusNum;
    }
}
